#!/bin/bash
cd `dirname $0`
#trap "rm -r root > /dev/null >& /dev/null" EXIT

if [ "$1" == "clean" ]; then
	rm -r root
	echo "Removed test root."
	exit 0
fi

clear

bin="stag"
conf=".stag.tags"

SCLWHITE="\e[0;37m"
SCWHITE="\e[1;37m"
SCLPURPLE="\e[0;35m"
SCRED="\e[1;31m"
SCLRED="\e[0;31m"
SCLGREEN="\e[0;32m"
SCGREEN="\e[1;32m"
SCNONE="\e[0m"
SCLBLUE="\e[0;36m"
SCYELLOW="\e[0;33m"

# lib test suite ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function header {
	echo -e $SCGREEN"$1"$SCNONE
}
function notice {
	echo -e $SCLBLUE"$1"$SCNONE
}
function message {
	echo -en $SCLPURPLE"$1: "$SCNONE
}
function checkstatus {
	s="$?"
	message "$1"
	$2 > "/dev/null" >& "/dev/null"
	if [ "$s" == "$3" ]; then
		echo -e $SCWHITE"[passed]$SCNONE"
	else
		echo -e $SCRED"[Failed]$SCLRED return status other than expected:\ncommand: $2\noutput:"
		$2
		echo -e $SCNONE
		exit 1
	fi
	
}
function checkfileexists {
	message "$1"
	if [ -e $2 ]; then 
		echo -e $SCWHITE"[passed]$SCNONE"
	else
		echo -e $SCRED"[Failed]$SCLRED file '$2' from `pwd` does not exist."$SCNONE
		exit 1
	fi
}
function assertmathchingoutput {
	message "$1"
	if [ `diff  <(echo "$2" ) <(echo "$3")` ]; then
		echo -e $SCRED"[Failed]$SCLRED Output does not match what was antisipated:"$SCNONE
		diff  <(echo "$2" ) <(echo "$3")
	else
		echo -e $SCWHITE"[passed]$SCNONE"
	fi
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



#specific
function getrelativerootpath {
	base=`pwd`
	base=`basename "$base"`
	if [ "$base" == "0" ]; then
		echo "../.."
	elif [ "$base" == "1" ]; then
		echo "../../.."
	else
		echo "../../../.."
	fi
}

function buildList {
	if [ "$1" == "absolute" ]; then 
		echo -n $testbasedir"/root/0/f0-"{0..2}" "
		echo -n $testbasedir"/root/0/1/f1-"{0..2}" "
		echo -n $testbasedir"/root/0/1/"{20..22}"/f2-"{0..2}" "
		if [ "$2" == "ln" ]; then
			echo -n $testbasedir"/root/0/1/20/f2-l"{0..2}" "
		fi
	elif [ "$1" == "relative" ]; then 
		base=`getrelativerootpath`
		echo -n $base"/root/0/f0-"{0..2}" "
		echo -n $base"/root/0/1/f1-"{0..2}" "
		echo -n $base"/root/0/1/"{20..22}"/f2-"{0..2}" "
		if [ "$2" == "ln" ]; then
			echo -n $base"/root/0/1/20/f2-l"{0..2}" "
		fi
	fi
}

function tests {
	notice "database is current working directory" #database in parent/database somewhere else
	checkstatus "Checking return status of version query" "$bin -ov" 0
	checkstatus "Attempting to initialize a database in 1 via absolute path" "$bin -oib $testbasedir/root/0/1" 0
	checkfileexists "Does database exist?" $testbasedir/root/0/1/$conf
	rm $testbasedir/root/0/1/$conf
	pwd
	checkstatus "Attempting to initialize a database in 1 via relative path" "$bin -oib root/0/1" 0
	checkfileexists "Does database exist?" $testbasedir/root/0/1/$conf

	#TODO add testing for /config 
	notice "changing into 0 to test adding files"
	cd "$testbasedir/root/0"
	$bin -ob ./1 -add `buildList relative` -tags test-suite
	
	# Adding db file in base directory
	
#	cd "$testbasedir/root/parent/base"
#	checkstatus "Initializing database in curent directory" "$bin -oi" 0 # TODO add all posible database locations
#	checkfileexists "Checking if database file exists" $conf
#	cd -
#	notice "adding files via relaive paths" 
#	rroot=`getrelativerootpath`
#	checkstatus "Checking that adding files outside the filespace is not permited" "$bin -add $rroot/parent/file1 -tags outcasts firsts" 1
#	$bin -add "$rroot/parent/base/subdir1/file8" -tags dbg
#	$bin -find
#
#comment='$testbasedir/root/parent/base
#$testbasedir/root/parent/base/subdir1
#$testbasedir/root/parent/base/subdir4'

#	validate "Checking by adding file1 with tags file and outcast" "$bin -oi"  # TODO add all types of paths (relative, absolute, local) with all types of directories

	#rm $conf
}


echo -e "Warning: 
*  Make sure you are in the directory in which this script was provided
   before continuing!
*  This will change any system wide settings set by stag.

Are you sure you want to proceed? [y]/n"
read prompt
if [ "$prompt" == "y" ] || [ "$prompt" == "" ]; then # how the @#$% do you add a global !
	echo -n ""; # the things you do
else
	echo "aborted."
	exit 0
fi

testbasedir=`pwd`

	#need to fix this
	cd ../src
	PATH=`pwd`:$PATH
	cd - > /dev/null


header "=== Initializing Test Environment ==="

notice "Removing old root directory if it exists.."
rm -r root/ >& /dev/null

notice "Creating directory stucture.."
mkdir -p $testbasedir"/root/0/1/"{20..22}

notice  "Populating directory stucture.."
touch $testbasedir"/root/0/f0-"{0..2}
touch $testbasedir"/root/0/1/f1-"{0..2}
touch $testbasedir"/root/0/1/"{20..22}"/f2-"{0..2}

notice  "Symbolicly linking to 20 and sub files.."
ln -s -T $testbasedir"/root/0/1/20" $testbasedir"/root/0/1/23"
for i in {0..2}; do
	ln -s -T $testbasedir"/root/0/1/23/f2-$i" \
	$testbasedir"/root/0/1/20/f2-l$i"
done

header  "=== Tests ==="
tests

#rm -r $testbasedir/root/
exit 0
